﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;
using StateGrid;
using System.Xml;
using StateGrid.GlobalVariables;

namespace StateGrid.XWLW
{
    /// <summary>
    /// Summary description for XWLW_DAO
    /// </summary>
    public class XWLW_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();

        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();

        /// <summary>
        /// 获取所有学位论文分类
        /// </summary>
        /// <returns></returns>
        public IList<XWLW_FL> GetAllXWLW_FL()
        {
            return sqlMapper.QueryForList<XWLW_FL>("SelectAllXWLW_FL", null);
        }

        /// <summary>
        /// 获取所有学位论文篇名信息
        /// </summary>
        /// <returns></returns>
        public IList<XWLW_PM> GetAllXWLW_PM()
        {
            return sqlMapper.QueryForList<XWLW_PM>("SelectAllXWLW_PM", null);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public XWLW_FL GetXWLW_FLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<XWLW_FL>("SelectXWLW_FLByFlbm", flbm);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="nr">分类名称</param>
        /// <returns></returns>
        public XWLW_FL GetXWLW_FLByNr(String nr)
        {
            return sqlMapper.QueryForObject<XWLW_FL>("SelectXWLW_FLByNr", nr);
        }

        /// <summary>
        /// 获取指定索引编码、年份、期号的篇名数量
        /// </summary>
        /// <param name="sybm">分类编码</param>
        /// <param name="nf">年份</param>
        /// <param name="qh">期号</param>
        /// <returns></returns>
        public int GetCountSomeXWLW_PM(String flbm,String nf,String qh)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String wjm=null;

            if (null == flbm || "".Equals(flbm))
            {
            }
            else
            {
                wjm = flbm;
                if (null!=nf&&nf.Length==4)
                {
                    wjm += '-' + nf;
                    if (null != qh && qh.Length == 2)
                    {
                        wjm += '-' + qh;
                    }
                }
            }

            if (null != wjm)
            {
                wjm += '%';
            }

            para.Add("wjm", wjm);
            return sqlMapper.QueryForObject<int>("CountSomeXWLW_PM", para);
        }


        /// <summary>
        /// 获取指定索引编码、年份、期号的片名数量
        /// </summary>
        /// <param name="sybm">索引编码</param>
        /// <param name="nf">年份</param>
        /// <param name="qh">期号</param>
        /// <param name="start">起始位置</param>
        /// <param name="end">结束位置</param>
        /// <returns></returns>
        public IList<XWLW_PM> GetSomeXWLW_PM(String sybm,String nf,String qh,int start,int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String wjm=null;

            if (null == sybm || "".Equals(sybm))
            {
            }
            else
            {
                wjm = sybm;
                if (null!=nf&&nf.Length==4)
                {
                    wjm += '-' + nf;
                    if (null != qh && qh.Length == 2)
                    {
                        wjm += '-' + qh;
                    }
                }
            }

            if (null != wjm)
            {
                wjm += '%';
            }

            para.Add("wjm", wjm);
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<XWLW_PM>("SelectSomeXWLW_PM", para);
        }

        /// <summary>
        /// 获取指定文件名的篇名信息
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <returns></returns>
        public XWLW_PM GetXWLW_PMByWjm(String wjm)
        {
            return sqlMapper.QueryForObject<XWLW_PM>("SelectXWLW_PMByWjm", wjm);
        }

        /// <summary>
        /// 插入一条分类信息
        /// </summary>
        /// <param name="xwlw_fl">分类信息</param>
        /// <returns></returns>
        public Boolean InsertXWLW_FL(XWLW_FL xwlw_fl)
        {
            Boolean flag = true;
            try
            {
                sqlMapper.Insert("InsertXWLW_FL", xwlw_fl);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 插入一条篇名信息
        /// </summary>
        /// <param name="xwlw_pm">篇名信息</param>
        /// <returns></returns>
        public Boolean InsertXWLW_PM(XWLW_PM xwlw_pm)
        {
            Boolean flag = true;
            OperationLogItem operationLog = this.GetOperationLogItem(xwlw_pm, "INSERT", xwlw_pm.dlr);
            try
            {
                sqlMapper.Insert("InsertXWLW_PM", xwlw_pm);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 更新一条分类信息
        /// </summary>
        /// <param name="xwlw_fl">要更新的分类信息</param>
        /// <returns></returns>
        public Boolean UpdateXWLW_FL(XWLW_FL xwlw_fl)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateXWLW_FL", xwlw_fl);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;

        }
        /// <summary>
        /// 更新一条篇名信息
        /// </summary>
        /// <param name="xwlw_pm">要更新的篇名信息</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public Boolean UpdateXWLW_PM(XWLW_PM xwlw_pm, String uname)
        {
            Boolean flag = true;
            int back=sqlMapper.Update("UpdateXWLW_PM", xwlw_pm);
            if (back <= 0)
            {
                flag = false;
            }
            if (flag)
            {
                OperationLogItem operationLog = this.GetOperationLogItem(xwlw_pm, "UPDATE", uname);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            return flag;
        }

        /// <summary>
        /// 按文件名删除篇名信息
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <returns></returns>
        public Boolean DeleteXWLW_FL(String flbm)
        {
            Boolean flag = true;
            int back = sqlMapper.Delete("DeleteXWLW_FLByFlbm", flbm);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 按文件名删除篇名信息
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        
        public int DeleteXWLW_PM(String wjm,String uname)
        {
            int result = 0;

            XWLW_PM xwlwPM = this.GetXWLW_PMByWjm(wjm);

            OperationLogItem operationLog = null;


            //切分文件名
            String[] str = wjm.Split('-');
            if (str.Length < 4)
            {
                return result;
            }
            //文件名前缀
            String wjmStr = str[0] + "-" + str[1] + "-" + str[2] + "-";

            //删除指定篇名及文件
            if (OperationFile.DeleteFile(wjm))
            {
                result = sqlMapper.Delete("DeleteXWLW_PMByWjm", wjm);
                operationLog = this.GetOperationLogItem(xwlwPM, "DELETE", uname);
                if (null != operationLog)
                {
                    this.operationLogDAO.insertLogItem(operationLog);
                }

            }

            ICollection<XWLW_PM> list = this.GetSomeXWLW_PM(str[0], str[1], str[2], 1, 100000);

            int ph = 0;
            String phString = null;
            if (null != list)
            {
                foreach (XWLW_PM temp in list)
                {
                    phString = FormatPH(ph++);
                    //如果当前文件名与新文件名一样则不做处理
                    if (temp.wjm.Equals(wjmStr + phString))
                    {
                        continue;
                    }
                    //如果当前文件名与新文件名不一致,则更新文件名及篇名信息
                    else
                    {
                        changeXWLW_PMWjm(temp.wjm, wjmStr + phString);
                    }
                }
            }
            return result;
        }





      
        /// <summary>
        /// 根据索引编码、年份、期号获取最大文件的篇名信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="nf">年份</param>
        /// <param name="qh">期号</param>
        /// <returns></returns>
        public XWLW_PM getMaxXWLW_PM(String flbm, String nf, String qh)
        {
            if (null == flbm || "".Equals(flbm))
            {
                return null;
            }
            if (null == nf || "".Equals(nf))
            {
                return null;
            }
            if (null == qh || "".Equals(qh))
            {
                return null;
            }
            //设置匹配文件名
            String wjm = flbm + "-" + nf + "-" + qh + "%";
            return sqlMapper.QueryForObject<XWLW_PM>("SelectMaxXWLW_PM", wjm);
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="xwlwPM">中文篇名信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(XWLW_PM xwlwPM, String opType, String uname)
        {
            if (null == xwlwPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "XWLW_PM";
            //全文页数
            operationLogItem.opPageCount = xwlwPM.ys;

            operationLogItem.opObjectID = "学位论文";
            // 操作涉及的书刊
            operationLogItem.opBookIn = "学位论文";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";

            //操作的内容
            String content = "('" + xwlwPM.flbm + "','" + xwlwPM.sybm + "','" + xwlwPM.wjm + "','" + xwlwPM.ywjm + "','" + xwlwPM.zwtm + "','" + xwlwPM.ywtm + "','"  + xwlwPM.zz + "','" + xwlwPM.ds + "','" + xwlwPM.lwjb + "','" + xwlwPM.xkzymc + "','" + xwlwPM.flh + "','" + xwlwPM.xwsydw + "','" + xwlwPM.lwtjrq + "','" + xwlwPM.lwdbrq+ "','" +xwlwPM.xwsyrq + "','" + xwlwPM.zwztc + "','" + xwlwPM.zwzy + "','" + xwlwPM.ywztc + "','" + xwlwPM.ywzy + "','" + xwlwPM.fltx + "','" + xwlwPM.nf + "','" + xwlwPM.cbrq + "','" + xwlwPM.ys + "','" + xwlwPM.gxrq + "','" + xwlwPM.qw + "')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }


        /// <summary>
        /// 修改已有的文件名。
        /// </summary>
        /// <param name="oldWjm">旧文件名</param>
        /// <param name="newWjm">新文件名</param>
        /// <returns>是否修改成功</returns>
        public bool changeXWLW_PMWjm(String oldWjm, String newWjm)
        {
            if (null == oldWjm || null == newWjm)
            {
                return false;
            }


            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("oldwjm", oldWjm);
            para.Add("newwjm", newWjm);
            bool result = true;
            try
            {
                sqlMapper.Update("ChangeXWLW_PM_wjm", para);
                //修改PDF文件名
                OperationFile.ReNameFile(oldWjm, newWjm);
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 规范篇号
        /// </summary>
        /// <param name="ph">篇号</param>
        /// <returns>4位篇号</returns>
        private String FormatPH(int ph)
        {
            String phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "0000" + phString;
            }
            //长度为2
            else if (phString.Length == 2)
            {
                phString = "000" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 4)
            {
                phString = "0" + phString;
            }
            return phString+".PDF";
        }

        public XWLW_DAO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}